package com.fenghongzhang.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

public class Main3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
    }

    public void notify(View view) {
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        final  Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("下载");
        builder.setProgress(100,0,false);
        final Notification build = builder.build();

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int value = 0;
            @Override
            public void run() {
                value+= 10;
                builder.setProgress(100,value,false);
                manager.notify(1,builder.build());
                if(value >= 100){
                    builder.setContentTitle("正在安装");
                    builder.setProgress(0,0,true);
                    manager.notify(1,builder.build());
                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    manager.cancel(1);
                    timer.cancel();
                }
            }
        },0,1000);
        manager.notify(1,build);

    }
}
